package com.hongchen.wechat.config;

import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.jfinal.MsgControllerAdapter;
import com.jfinal.weixin.sdk.msg.in.InImageMsg;
import com.jfinal.weixin.sdk.msg.in.InTextMsg;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
import com.jfinal.weixin.sdk.msg.in.event.InQrCodeEvent;
import com.jfinal.weixin.sdk.msg.in.speech_recognition.InSpeechRecognitionResults;
import com.jfinal.weixin.sdk.msg.out.OutImageMsg;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;

/**
 * Created by 3426624897@qq.com on 2018/2/8 0008.
 */
public class WeixinMsg extends MsgControllerAdapter {

    static Log logger = Log.getLog(WeixinMsg.class);
    private static final String helpStr = "你好！";

    //  关注/取消关注事件
    protected void processInFollowEvent(InFollowEvent inFollowEvent) {
        if (InFollowEvent.EVENT_INFOLLOW_SUBSCRIBE.equals(inFollowEvent.getEvent())) {
            logger.debug("关注：" + inFollowEvent.getFromUserName());
            OutTextMsg outMsg = new OutTextMsg(inFollowEvent);
            // String s=getPara("appId");
            // if(s.equals("wxc04810a824175f8f")==true){
            // outMsg.setContent(helpStr);
            // }
            render(outMsg);
        }
        // 如果为取消关注事件，将无法接收到传回的信息
        if (InFollowEvent.EVENT_INFOLLOW_UNSUBSCRIBE.equals(inFollowEvent.getEvent())) {
            //logger.debug("取消关注：" + inFollowEvent.getFromUserName());
        }
    }

    /**
     * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
     * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
     */
    public ApiConfig getApiConfig() {
        ApiConfig ac = new ApiConfig();
        // 配置微信 API 相关常量
        ac.setToken("orange1");
        //获取带参数链接的Id & 秘钥
        ac.setAppId(getPara("appId"));
        ac.setAppSecret(getPara("appSecret"));
        /**
         *  是否对消息进行加密，对应于微信平台的消息加解密方式：
         *  1：true进行加密且必须配置 encodingAesKey
         *  2：false采用明文模式，同时也支持混合模式
         */
        ac.setEncryptMessage(false);
        ac.setEncodingAesKey("3UkV6iaYWxlb83jfD7w2gDMvJXXLEqzPfwErYoncBYB");
        return ac;
    }

    /**
     * 获取用户用户发送的信息
     * @param inTextMsg
     */
    protected void processInTextMsg(InTextMsg inTextMsg) {

        String msgContent = inTextMsg.getContent().trim();

        //用户输入001返回对应的东西
        if(msgContent.equals("大富翁")) {
            renderOutTextMsg("\t<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx61046b01eca0048b&redirect_uri=http://chengdong4.tunnel.echomod.cn/wxcs&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect\">大富翁</a>");
        }else if(msgContent.equals("2")){
            renderOutTextMsg("\t<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx61046b01eca0048b&redirect_uri=http://chengdong4.tunnel.echomod.cn/wxcs&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect\"></a>");
        }else if(msgContent.equals("3")){
            renderOutTextMsg("\t<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx61046b01eca0048b&redirect_uri=http://chengdong4.tunnel.echomod.cn/buguess&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect\">楼盘猜想</a>");
        }else{
            renderOutTextMsg(msgContent);
        }
    }

    protected void processInQrCodeEvent(InQrCodeEvent inQrCodeEvent) {
        //直接获取openid
        String openid = this.getPara("openid");

        //获取二维码参数（发送者微信id）
        String sst = inQrCodeEvent.getEventKey();

        int cc = sst.length() - 8;
        String b = "";
        if(sst.length()>=1){
            b= sst.substring(sst.length() - cc, sst.length());//不进行截取的话会在id前面加9为数字
        }else{
            b=sst;
        }

        if (InQrCodeEvent.EVENT_INQRCODE_SUBSCRIBE.equals(inQrCodeEvent.getEvent())) {
            logger.debug("扫码未关注：" + inQrCodeEvent.getFromUserName());
            //发送关注语
            renderOutTextMsg(helpStr);
        }
        if (InQrCodeEvent.EVENT_INQRCODE_SCAN.equals(inQrCodeEvent.getEvent())){// inQrCodeEvent.getEventKey()  获取二维码带参参数
            logger.debug("扫码已关注：" + inQrCodeEvent.getFromUserName());
            //发送关注语
            renderOutTextMsg(helpStr);
        }

    }

    @Override
    protected void processInMenuEvent(InMenuEvent inMenuEvent) {
        logger.debug("菜单事件：" + inMenuEvent.getFromUserName());
        OutTextMsg outMsg = new OutTextMsg(inMenuEvent);
        outMsg.setContent("菜单事件内容是：" + inMenuEvent.getEventKey());
        System.out.println(inMenuEvent.getEventKey());
        //renderOutTextMsg(inMenuEvent.getEventKey());
    }

    protected void processInImageMsg(InImageMsg inImageMsg) {
        OutImageMsg outMsg = new OutImageMsg(inImageMsg);
        // 将刚发过来的图片再发回去
        outMsg.setMediaId(inImageMsg.getMediaId());
        System.out.println(inImageMsg.getPicUrl());//获取用户发送图片的url
        render(outMsg);
    }

    /**
     * 接受用户语音  可用
     * @param inSpeechRecognitionResults
     */
    protected void processInSpeechRecognitionResults(InSpeechRecognitionResults inSpeechRecognitionResults) {
        String yuyin=inSpeechRecognitionResults.getRecognition();
        System.out.println(yuyin);
        renderOutTextMsg(yuyin);
    }
}
